package utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 功能描述:
 *
 * @ClassName: CalendarUtil
 * @Author: xiaoqianh丶
 * @Date: 2019-05-28 11:10
 * @Version: V1.0
 */
public class CalendarUtil {
    /**
     * 获取今日开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setDayRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        condition.put("startTime",calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY,24);
        condition.put("stopTime",calendar.getTime());
        return condition;
    }


    /**
     * 获取今日开始时间和结束时间(指定时间 时间格式 yyyy-MM-dd HH:mm:ss)
     * @param str
     * @return
     */
    public static Map<String,Date> setDayRange(String str){
        Map<String, Date> condition = new HashMap<>();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        calendar.set(Calendar.MILLISECOND,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        condition.put("startTime",calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY,24);
        condition.put("stopTime",calendar.getTime());
        return condition;
    }


    /**
     * 获取昨天开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setYesterdayRange(Map<String ,Date> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        condition.put("stopTime",calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY,-24);
        condition.put("startTime",calendar.getTime());
        return condition;
    }

    /**
     * 获得最近三天的开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setThreeRange(Map<String ,Date> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.HOUR_OF_DAY,24);
        condition.put("stopTime",calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY,-72);
        condition.put("startTime",calendar.getTime());
        return condition;
    }

    /**
     * 获得近一周的开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setDaySevenRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.HOUR_OF_DAY,24);
        condition.put("stopTime",calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY,-168);
        condition.put("startTime",calendar.getTime());
        return condition;
    }

    /**
     * 获得指定时间近一周的开始时间和结束时间
     * @param
     * @return
     */
    public static Map<String,Date> setDaySevenRange(String str){
        Map<String, Date> condition = new HashMap<>();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.HOUR_OF_DAY,24);
        condition.put("stopTime",calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY,-168);
        condition.put("startTime",calendar.getTime());
        return condition;
    }

    /**
     * 获得近一月的开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setDayTRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.HOUR_OF_DAY,24);
        condition.put("stopTime",calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY,-720);
        condition.put("startTime",calendar.getTime());
        return condition;
    }

    /**
     * 获取本周开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setWeekRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        condition.put("stopTime",calendar.getTime());
        return condition;

//        或者
//        int week = calendar.get(Calendar.DAY_OF_WEEK)-2;
//        calendar.add(Calendar.DATE, -week);
//        condition.put("startTime",dateToString(calendar.getTime()));
//        calendar.add(Calendar.DATE, 7-week);
//        condition.put("stopTime",dateToString(calendar.getTime()));
    }


    /**
     * 获取指定时间内本周开始时间和结束时间
     * @param str
     * @return
     */
    public static Map<String,Date> setWeekRange(String str){

        Map<String, Date> condition = new HashMap<>();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        condition.put("stopTime",calendar.getTime());
        return condition;

//        或者
//        int week = calendar.get(Calendar.DAY_OF_WEEK)-2;
//        calendar.add(Calendar.DATE, -week);
//        condition.put("startTime",dateToString(calendar.getTime()));
//        calendar.add(Calendar.DATE, 7-week);
//        condition.put("stopTime",dateToString(calendar.getTime()));
    }


    /**
     * 获取上周开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setLastWeekRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        condition.put("stopTime",calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, -7);
        condition.put("startTime",calendar.getTime());
        return condition;
    }

    /**
     * 获取指定时间本月开始时间和结束时间
     * @param str
     * @return
     */
    public static Map<String,Date> setMonthRange(String str){
        Map<String, Date> condition = new HashMap<>();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        condition.put("stopTime",calendar.getTime());
        return condition;
    }
    /**
     * 获取本月开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setMonthRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        condition.put("stopTime",calendar.getTime());
        return condition;
    }


    /**
     * 获取上月开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setLastMonthRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        condition.put("stopTime",calendar.getTime());
        return condition;
    }

    /**
     * 获取本季度开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setQuarterRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 		calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                calendar.set(Calendar.MONTH, 0);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                calendar.set(Calendar.MONTH, 3);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                calendar.set(Calendar.MONTH, 6);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                calendar.set(Calendar.MONTH, 9);
            }
            calendar.set(Calendar.DATE, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.MONTH, 3);
        condition.put("stopTime",calendar.getTime());
        return condition;
    }

    /**
     * 获取上季度开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setLastQuarterRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                calendar.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                calendar.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                calendar.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                calendar.set(Calendar.MONTH, 9);
            calendar.set(Calendar.DATE, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.MONTH, -3);
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.MONTH, 3);
        condition.put("stopTime",calendar.getTime());
        return condition;
    }


    /**
     * 获取本年开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setYearRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.YEAR, 1);
        condition.put("stopTime",calendar.getTime());
        return condition;
    }

    /**
     * 获取上一年开始时间和结束时间
     * @param condition
     * @return
     */
    public static Map<String,Date> setLastYearRange(Map<String ,Date> condition){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.YEAR, -1);
        condition.put("startTime",calendar.getTime());
        calendar.add(Calendar.YEAR, 1);
        condition.put("stopTime",calendar.getTime());
        return condition;
    }

    /**
     * （"startTime" -> "Fri Jun 08 00:00:00 CST 2018"）String的Date转为String类型的date
     */
    public String strToformatDate(String date){
        String str="";
        Date date1=new Date();
        try {
            DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            date1 = df.parse(date);
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            str = df.format(date1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return str;
    }

    /**
     * （"startTime" -> "Fri Jun 08 00:00:00 CST 2018"）String的Date转为时间戳
     */
    public long toformatDate(String date){
        String str="";
        Date date1=new Date();
        try {
            DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            date1 = df.parse(date);
            System.err.println(date1.getTime()/1000);
        }catch (Exception e){
            e.printStackTrace();
        }
        return date1.getTime()/1000;
    }



    /**
     * 得到n天之后是周几
     * @param days
     * @return
     */
    public static String getAfterDayWeek(String days) {
        int daysInt = Integer.parseInt(days);
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);
        return dateStr;
    }

    /**
     * 得到n天之后的当前时间
     * @param days
     * @return
     */
    public static String getAfterDayDate(String days) {
        int daysInt = Integer.parseInt(days);
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        return dateStr;
    }

    /**
     * 得到n天之后的指定时间
     * @param days
     * @return
     */
    public static Date getAfterDayDate(String days,Date date) {
        int daysInt = Integer.parseInt(days);
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(date);
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        return canlendar.getTime();
    }


    /**
     * 得到n月之后的指定时间
     * @param days
     * @return
     */
    public static Date getAfterMonthDate(String days,Date date) {
        int daysInt = Integer.parseInt(days);
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(date);
        canlendar.add(Calendar.MONTH, daysInt); // 日期减 如果不够减会将月变动
        return canlendar.getTime();
    }
    /**
     * 得到几分钟后的时间
     * @param date minute（可为正负）
     * @return
     */
    public static Date getAfterTimeDate(Date date,int minute) {
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(date);
        canlendar.add(Calendar.MINUTE, minute); // 日期减 如果不够减会将月变动
        return canlendar.getTime();
    }

    /**
     * 时间相减得到天数（开始时间和结束时间可任意大小）
     * @param beginDateStr（格式为"yyyy-MM-dd"，也可为"yyyy-MM-dd hh:mm:ss"）
     * @param endDateStr
     * @return  long
     */
    public static long getDaySub(String beginDateStr,String endDateStr){
        long day=0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date beginDate = null;
        Date endDate = null;
        try {
            beginDate = format.parse(beginDateStr);
            endDate= format.parse(endDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
        //System.out.println("相隔的天数="+day);
        return day;
    }

    private String dateToString(Date date){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

}
